<?php


namespace app\admin\controller\v1;


use app\admin\model\BloggerModel;
use app\admin\model\BlogSettingModel;
use app\admin\Validate\setting\SaveBlogSettingValidate;
use app\admin\Validate\setting\UpdateBloggerValidate;
use app\BaseController;
use app\common\Token;

/**
 * 博客相关设置
 * Class BlogSetting
 * @package app\admin\controller\v1
 */
class BlogSetting extends BaseController
{
    /**
     * 更新相关设置
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveBlogSetting()
    {
        Token::checkToken($this->request);
        $param = (new SaveBlogSettingValidate())->validateParam();
        $model = BlogSettingModel::find($param['id']);
        $re = $model->save($param);
        if($re){
            return successJson(true);
        }
        return errorJson(60004,"更新失败");
    }

    /**
     * 获取设置
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBlogSetting() {
        Token::checkToken($this->request);
        $arr = (new BlogSettingModel())->select()->toArray();
        return successJson($arr[0]);
    }

    /**
     * 更新博主信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateBloggerInfo() {
        Token::checkToken($this->request);
        $param = (new UpdateBloggerValidate())->validateParam();
        $result = BloggerModel::find(1)->toArray();
        $old_content = $result['blogger_content'];
        $param['id'] = 1;
        $param['old_blogger_content'] = $old_content;
        $param['update_time'] = getDateTime();
        $bool = BloggerModel::update($param);
        if($bool){
            return successJson('更新成功');
        }
        return errorJson(60003,'更新失败');
    }

    /**
     * 获取博主信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBloggerInfo() {
        Token::checkToken($this->request);
        $result = BloggerModel::find(1)
            ->toArray();
        return successJson($result);
    }

}

